﻿using UnityEngine;
using System.Collections.Generic;
namespace Room
{
    public class RoomBase : MonoBehaviour
    {
        public SceneCamera sceneCamera;
        public RoomCharactorManager charactorManager;

        public RoomSmallMap smallMap;
        public ViewController viewController;
        public RoomLODManager lodManager;
        public RoomNetBase netManager;
        
        protected bool inited = false;
        protected Vector3 cameraDelta = new Vector3(0, 9f, -8f);
        protected float cameraAngle = 45f;
        protected float cameraFOV = 38f;

        public RoomUIManager uiManager
        {
            get
            {
                return RoomUIManager.Instance;
            }
        }
        public delegate void EventUIFuncHandler();
        public event EventUIFuncHandler uiFuncs;

        public bool foreGround  = false;

        public virtual SceneType GetSceneType()
        {
            return SceneType.LOGIN;
        }

        public virtual NetWork.ServerType GetServerType() {
            return NetWork.ServerType.none;
        }

        //初始化
        public virtual void Init()
        {
            sceneCamera = new SceneCamera(CameraTool.Instance.sceneCamera, null);
            charactorManager = new RoomCharactorManager();
            charactorManager.room = this;

            lodManager = new RoomLODManager();
            lodManager.Init(this);
            
            smallMap = new RoomTownSmallMap();
            smallMap.Init(this);

            InitRoomNet();

            viewController = new ViewController(charactorManager);
            charactorManager.onCharactorStartCallback += OnNewCharactorCreate;
            charactorManager.onCharactorStopCallback += OnCharactorDestroy;

            Time.timeScale = 1f;
        }

        public virtual void InitRoomNet()
        {

        }

        //进入场景
        public void Enter(params System.Object[] param)
        {
            OnEnter(param);
        }
        protected virtual void OnEnter(params System.Object[] param)
        {
            Time.timeScale = 1f;
        }
        //离开场景
        public void Leave()
        {
            Background();
            OnLeave();
        }
        protected virtual void OnLeave()
        {
            if(smallMap != null)
            {
                smallMap.HideMiniMap();
            }
        }
        //进入后台
        public void Background()
        {
            if(foreGround)
            {
                foreGround = false;
                charactorManager.Pause();
                OnBackground();

                //gameObject.SetActive(false);
            }
        }

        protected virtual void OnBackground()
        {

        }
        //进入前台
        public void Foreground()
        {
            if(!foreGround)
            {
                //gameObject.SetActive(true);

                foreGround = true;
                OnForeground();
                charactorManager.Resume();
            }
        }

        protected virtual void OnForeground()
        {

        }

        public virtual void Update()
        {
            if(!foreGround)
            {
                return;
            }
            Tick();
        }

        protected virtual void Tick()
        {

        }

        public void LateUpdate()
        {
            if (!foreGround)
            {
                return;
            }
            if (sceneCamera != null)
            {
                sceneCamera.Tick();
            }
            if(uiManager!=null)
            {
                uiManager.Tick();
            }
            charactorManager.LateTick();
            LateTick();
            if(uiFuncs!=null)
            {
                uiFuncs();
                uiFuncs = null;
            }
        }

        protected virtual void LateTick()
        {
            
        }

        public virtual void OnMainCharactorLoaded(Transform mc)
        {
            sceneCamera.SetTarget(mc);
            
        }

        public virtual void PlayerSay(string playerId, string word)
        {
            //TODO 获取对应的玩家，传递说话方法
        }

        protected virtual void OnNewCharactorCreate(RoomCharactor rc)
        {
            if (rc.type == CharactorType.NormalNPC
                || rc.type == CharactorType.ActorNPC
                || rc.type == CharactorType.StoryNPC)
            {
                smallMap.miniMapComponent.LoadFixedCharactor(rc.name, rc);
            }
            else if (rc.type == CharactorType.Area)
            {
                smallMap.miniMapComponent.LoadFixedCharactor(rc.name, rc);
            }
            else
            {
                smallMap.miniMapComponent.LoadLiveCharactor(rc.name, rc);
            }
            
        }

        protected virtual void OnCharactorDestroy(RoomCharactor rc)
        {
            smallMap.miniMapComponent.UnloadCharactor(rc.name);
        }

        public virtual void LoadNPCDataFromTable(string name)
        {
            var listNpcs = Dict.DictDataManager.Instance.tableNPCDao.GetByOneIndex(name);
            foreach (var npc in listNpcs)
            {
                switch (npc.type)
                {
                    case (int)NPCType.Normal:
                    case (int)NPCType.Story:
                        charactorManager.CreateNPC(npc.npcId);
                        break;
                }
            }
        }
        public virtual void LoadAreaDataFromTable(string name)
        {
            var listAreas = Dict.DictDataManager.Instance.tableAreaDao.GetByOneIndex(name);
            foreach (var area in listAreas)
            {
                if (area.sceneName.Equals(name))
                {
                    charactorManager.CreateArea(area);
                }
            }
        }

        public void RoomLoadEnd()
        {
            DataMessageTool.AddMessage(DataMessage.ROOM_SELF_CREATED, this);
        }
    }
}